using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Collections;
using PrintDataGrid;
using System.Collections.Specialized;

namespace eBirdAttend
{
    public partial class DailyTransactionRpt : UserControl
    {
        public string strCon = Properties.Settings.Default["ConString"].ToString();
        SqlConnection sqlCon = new SqlConnection();
        SqlCommand sqlCmd = new SqlCommand();

        public DailyTransactionRpt()
        {
            InitializeComponent();
            sqlCon.ConnectionString = strCon;
            sqlCmd.Connection = sqlCon;
        }

        private void btnShow_Click(object sender, EventArgs e)
        {
            
            //string strOrder = "";
            //if (cmbOrderBy.SelectedIndex == 1) 
            //{
            //    strOrder = "branch";
            //}
            //else if (cmbOrderBy.SelectedIndex == 2)
            //{
            //    strOrder = "Department";
            //}

           // MessageBox.Show(System.IO.Directory.GetCurrentDirectory().ToString());
            try
            {
                sqlCmd.Parameters.Clear();
                SqlParameter param = sqlCmd.Parameters.AddWithValue("@date", dateTimePicker1.Value.Date.ToString("MM/dd/yyyy"));

                sqlCmd.CommandText = "DailyTransactionReport";
                sqlCmd.CommandType = CommandType.StoredProcedure;
                //sqlCmd.Parameters.Add("@brid", "1");
                //sqlCmd.Parameters.Add("@deptid", "1");
                if (sqlCon.State == ConnectionState.Closed)
                sqlCon.Open();
                sqlCmd.ExecuteNonQuery();
                sqlCon.Close();
                if (sqlCon.State == ConnectionState.Closed)
                sqlCon.Open();

                sqlCmd.CommandText = "DailyTransaction_RPT";
                sqlCmd.CommandType = CommandType.StoredProcedure;
                sqlCmd.Parameters.Clear();
                sqlCmd.ExecuteNonQuery();
                DataSet ds = new DataSet();
                SqlDataAdapter sqlAdap = new SqlDataAdapter(sqlCmd);
                sqlAdap.Fill(ds);
                dataGridView1.DataSource = ds;
                dataGridView1.DataMember = ds.Tables[0].ToString();
                sqlCon.Close();
            }
            catch (SqlException ex)
            {
            }
        }

        private void DailyTransactionRpt_Load(object sender, EventArgs e)
        {
            cmbGroup.SelectedIndex = 0;
            
            //branchTableAdapter1.Fill(branchDataset.Branch);              
            //departmentTableAdapter1.Fill(departmentDataset.Department);
            //cmbOrderBy.SelectedIndex = 0;

            departmentDataset.Clear();
            departmentTableAdapter1.Fill(departmentDataset.Department);

            DataTable table1 = new DataTable("Table1");

            table1 = departmentDataset.Tables[0].Clone();

            // Add some rows.
            DataRow row;
            row = table1.NewRow();
            row["depid"] = 0;
            row["depname"] = "[Select]";
            row["depcmpid"] = "1";
            row["dephod"] = "Select";
            table1.Rows.Add(row);

            // Accept changes.
            table1.AcceptChanges();

            departmentDataset.Tables[0].Constraints.Clear();
            DataTable table2 = departmentDataset.Tables[0];

            table1.Merge(table2);

            cmbDept.DataSource = table1;
            cmbDept.DisplayMember = "depname";
            cmbDept.ValueMember = "depid";

            branchTableAdapter1.Fill(branchDataset.Branch);
            table1 = new DataTable("Table1");

            table1 = branchDataset.Tables[0].Clone();

            // Add some rows.
            row = table1.NewRow();
            row["brbranchid"] = 0;
            row["brcmpid"] = 0;
            row["brname"] = "[Select]";

            table1.Rows.Add(row);

            // Accept changes.
            table1.AcceptChanges();

            branchDataset.Tables[0].Constraints.Clear();
            table2 = branchDataset.Tables[0];
            table1.Merge(table2);

            cmbBranch.DataSource = table1;
            cmbBranch.DisplayMember = "brname";
            cmbBranch.ValueMember = "brbranchid";

          
            cmbDept.SelectedIndex = 0;
            cmbBranch.SelectedIndex = 0;

            cmbBranch.Focus();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btnShowCrystalReport_Click(object sender, EventArgs e)
        {
        //    ShowCrsytalReport _ShowCrystalReport = new ShowCrsytalReport();
        //    _ShowCrystalReport.strReportName = "Daily";
        //    _ShowCrystalReport.Show();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            PrintDGV printdgv = new PrintDGV();
            printdgv.Print_DataGridView(dataGridView1);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int j = 1;
            int i;
            string strReportTemplate = Application.StartupPath.ToString() + "\\" +
                Properties.Settings.Default.DailyTransactionReport.ToString();
            //MessageBox.Show(strReportTemplate);
            if (strReportTemplate == "")
            {
                MessageBox.Show("Report Template Name Missing");
                return;
            }

            //ConfigurationManager.AppSettings["AttendanceReport"].ToString();
            // ConfigurationSettings.AppSettings["AttendanceReport"].ToString();

            StringCollection strHeaderCol = new StringCollection();
            EbExcelAuto exelAttnReport = new EbExcelAuto();
            string curColText = "";
            exelAttnReport.Init();
            exelAttnReport.OpenExcelFile(strReportTemplate);
            exelAttnReport.FillExcellSheetFromDatagrid("#START", dataGridView1, "EmpId");
            exelAttnReport.SaveAs(null);
            exelAttnReport.Close();
        }
    }
}
